/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ideal.Interface;

import com.ideal.log.Log;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties;


/**
 *
 * @author Huyu
 */
public class MainConfig {
    public static String m_idealUrl = "http://192.168.1.106/test.php";//ideal私有云服务器
    public static int m_LedInfodelay = 30;//获取led信息延迟,秒为单位
    public static int m_LedInfoinertval = 10;//获取led信息间隔,秒为单位

    public static void init(String dir) {
        String Path = dir + "Config.ini";
        File f = new File(Path);
        if (!f.exists()) {
            Log.logger.info("File not found ,the path is " + Path);
            return;
        }

        InputStream inputStream = null;
        Properties p = new Properties();
        try {
            inputStream = new FileInputStream(f);
            p.load(inputStream);
            String httpUrl = p.getProperty("httpUrl");
            int delay = getTimeAttri(p, "LedInfodelay");
            int inertval = getTimeAttri(p, "LedInfoinertval");
            int ListLedIpdelay = getTimeAttri(p, "ListLedIpdelay");
            int ListLedIpinertval = getTimeAttri(p, "ListLedIpinertval");

            if (httpUrl != null) {
                m_idealUrl = httpUrl;
            }
            if (delay != -1) {
                m_LedInfodelay = delay;
            }
            if (inertval != -1) {
                m_LedInfoinertval = inertval;
            }


            Log.logger.info("Config attri m_httpUrl is " + m_idealUrl);
            Log.logger.info("Config attri m_LedInfodelay is " + m_LedInfodelay);
            Log.logger.info("Config attri m_LedInfoinertval is " + m_LedInfoinertval);
           
        } catch (Exception e) {
            Log.logger.info("the Exception is " + e.getMessage());
        }
    }

    public static int getTimeAttri(Properties p, String AttriName) {
        int value = -1;
        String Attri = p.getProperty(AttriName);
        if (Attri == null) {
            return value;
        }
        if (!Attri.contains("*")) {
            return Integer.valueOf(Attri);
        }
        if (Attri.contains("*")) {
            value = 1;
            String[] AttriList = Attri.split("[*]");
            for (int i = 0; i < AttriList.length; i++) {
                value = value * Integer.valueOf(AttriList[i]);
            }
        }
        return value;
    }
}
